import { getShopDetails, getGoodsList, getCommondData } from "@/api/shop"

const state = {
    shopData: {},
    goodsList: [],
    rateLIst: []
}

const getters = {
    navsList(state) {
        return state.goodsList.map(v => v.name)
    },
    goodRate(state) {
        return state.rateLIst.filter(v => {
            return v.score > 3
        })
    },
    badRate(state) {
        return state.rateLIst.filter(v => {
            return v.score <= 3
        })
    }
}

const mutations = {
    SHOP_DETAILS(state, data) {
        state.shopData = data
    },
    GOODS_LIST(state, data) {
        state.goodsList = data
    },
    RATE_LIST(state, data) {
        state.rateLIst = data
    }

}

const actions = {
    A_getShopData({ commit }) {
        return new Promise(async resolve => {
            let res = await getShopDetails()
            let { data } = res.data
            commit('SHOP_DETAILS', data)
        })
    },
    A_getGoodsLIst({ commit }) {
        return new Promise(async resolve => {
            let res = await getGoodsList()
            console.log(res);
            let { data } = res.data
            commit('GOODS_LIST', data)
            resolve()
        })
    },
    A_RATELIST({ commit }) {
        return new Promise(async resolve => {
            let res = await getCommondData()
            console.log(res);
            let { errno, data } = res.data
            if (errno == 0) {
                commit('RATE_LIST', data)
            }
            resolve()
        })

    }
}


export default {
    namespaced: true,
    state,
    getters,
    mutations,
    actions
}

